package gui;

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import objetos.Cobra;
import objetos.Posicao;
import objetos.Snake;
import policia.Policia;

public class Gui {

	private JFrame frame;
	private Field field ;
	private ArrayList<Snake> lista_cobras;

	public Gui() {

		frame = new JFrame("Jogo Snake");
		addComponentPaint();
		lista_cobras = new ArrayList<>();

		//		Cobra cobra1 = new Cobra(5, field);
		//		Cobra cobra2 = new Cobra(6, field);
		//		Cobra cobra3 = new Cobra(6, field);
		//		Cobra cobra4 = new Cobra(6, field);

		frame.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				int c = ((e.getX()-8) - (e.getX()-8)%20)/20;
				int r = ((e.getY()-30) - (e.getY()-30)%20)/20;
				int index = r * 20 + c;

				for(Snake snake: lista_cobras)
					if(snake.getCorpo().contains(field.getPosicoes().get(index))){
						//Definir objetivo
					}



			}
		});

		frame.getContentPane().add(field);
		frame.getContentPane().setPreferredSize(new Dimension(field.getNUM_COL() * field.getSNAKE_SIZE(), field.getNUM_LIN() * field.getSNAKE_SIZE()));
		frame.pack();
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//		cobra1.start();
		//		cobra2.start();
		//		cobra3.start();
		//		cobra4.start();


	}

	private void addComponentPaint() {

		//		int NUM_COL = Integer.parseInt(JOptionPane.showInputDialog("Insira o numero de colunas que deseja:"));
		//		System.out.println("Numero colunas: " + NUM_COL);
		//		int NUM_LIN = Integer.parseInt(JOptionPane.showInputDialog("Insira o numero de linhas que deseja:"));;
		//		System.out.println("Numero linhas: " + NUM_LIN);
		//		int SQUARE_SIZE = Integer.parseInt(JOptionPane.showInputDialog("Insira tamanho do quadrado que deseja:"));;
		//		System.out.println("Tamanho do quadrado: " + SQUARE_SIZE);

		//field = new Field(NUM_COL, NUM_LIN, 20);
		field = new Field(20, 20, 20);
	}

	public Field getField() {
		return field;
	}

	public void adicionar_Cobra(Snake snake){
		lista_cobras.add(snake);
	}




	public static void main(String[] args) {
		Gui gui = new Gui();
		Policia p = new Policia(gui.getField());

		for(int i= 0; i < 5; i++){
			p.posicaoAleatoria().setApple();
		}
		Snake snake = new Snake(p, 5);
		snake.start();
//		for(int i= 0; i<3; i++){
//			Snake snake = new Snake(p, 5);
//			snake.start();
//			gui.adicionar_Cobra(snake);
//		}


	}
}
